//
//  MCAlertView.swift
//  StoreManage
//
//  Created by MC on 2018/8/10.
//  Copyright © 2018年 MC. All rights reserved.
//

import Foundation

extension UIAlertController {

    
    //在指定视图控制器上弹出普通消息提示框
    static func showAlert(message: String, in viewController: UIViewController) {
        let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "确定", style: .cancel))
        viewController.present(alert, animated: true)
    }
    
    //在根视图控制器上弹出普通消息提示框
    static func showAlert(message: String) {
        if let vc = UIApplication.shared.keyWindow?.rootViewController {
            showAlert(message: message, in: vc)
        }
    }
    
    
    
    //在指定视图控制器上弹出确认框
    static func showConfirmOn(_ vc: UIViewController, title: String = "",message: String, confirm: ((UIAlertAction)->Void)?,cancel: ((UIAlertAction)->Void)? = nil) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: cancel))
        alert.addAction(UIAlertAction(title: "确定", style: .default, handler: confirm))
        vc.present(alert, animated: true)
    }
    
    
    // 通过数组，自定义UIAlertAction的个数和内容
    static func showActionSheet(vc : UIViewController,message:String,items:[String],custom : ((Int,String) -> Void)?) {
      
        let alter = UIAlertController.init(title: message, message: nil, preferredStyle: .actionSheet)
        let cancle = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.cancel) { (a) in }
        
        var index = 0
        for item in items {
            let i = index
            let confirm = UIAlertAction.init(title: item, style: UIAlertActionStyle.default) { (b) in
                custom?(i,item)
            }
            alter.addAction(confirm)
            index += 1
        }
        alter.addAction(cancle)
        vc.present(alter, animated: true, completion: nil)
    }
    
    

    
    // 拍照或者相册选择的ActionSheet
    
    // 微信/朋友圈的ActionSheet
}




//MARK: -UIAlertController
typealias AlterConfirm = () -> Void
func MCAlterController_twoOptions(vc : UIViewController?,message:String,confirm:AlterConfirm?,cancle : AlterConfirm?) {
    let alter = UIAlertController.init(title: message, message: "", preferredStyle: UIAlertControllerStyle.alert)
    let cancle = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.cancel) { (a) in
        if cancle != nil {
            cancle!()
        }
    }
    let confirm = UIAlertAction.init(title: "确定", style: UIAlertActionStyle.default) { (b) in confirm!() }
    alter.addAction(cancle)
    alter.addAction(confirm)
    if vc != nil {
        vc!.present(alter, animated: true, completion: nil)
    }
}

func MCAlterController_oneOptions(vc : UIViewController?,message:String,cancle :AlterConfirm?) {
    let alter = UIAlertController.init(title: message, message: "", preferredStyle: UIAlertControllerStyle.alert)
    let cancle = UIAlertAction.init(title: "我知道了", style: UIAlertActionStyle.cancel) { (a) in
        
        if cancle != nil {
            cancle!()
        }
    }
    alter.addAction(cancle)
    
    if vc != nil {
        vc!.present(alter, animated: true, completion: nil)
    }
    
}




